//    Copyright (C) Clear Office.  All rights reserved.
using System;
using Office.Core.Serialization;

namespace Office.Spreadsheet.PivotTable
{
    internal class DataField : IOfficeXmlSerializable
    {
        private string _name;
        private int _fld;
        private DataConsolidateFunction _subtotal;
        private ShowDataAs _showDataAs;
        private int _baseField;
        private int _baseItem;
        private uint? numFmtId;
        private ExtensionList _extLst;

        public void ReadXml(ReadContext context)
        {
            using (var c = context.Read("dataField"))
            {
                _name = c.GetOptionalString("name");
                _fld = c.GetUIntegerAsInteger("fld");
                _subtotal = c.GetOptionalEnum<DataConsolidateFunction>("subtotal", DataConsolidateFunction.Sum);
                _showDataAs = c.GetOptionalEnum<ShowDataAs>("showDataAs", ShowDataAs.Normal);
                _baseField = c.GetOptionalInteger("baseField", -1);
                _baseItem = c.GetOptionalInteger("baseItem", 1048832);
                numFmtId = c.GetOptionalUInteger("numFmtId");
                this._extLst = c.ReadOptionalElement<ExtensionList>("extLst");
            }
        }

        public void WriteXml(WriteContext context)
        {
            using (var c = context.Write("dataField"))
            {
                c.SetOptionalString("name", _name);
                c.SetUIntegerAsInteger("fld", _fld);
                c.SetOptionalEnum<DataConsolidateFunction>("subtotal", _subtotal, DataConsolidateFunction.Sum);
                c.SetOptionalEnum<ShowDataAs>("showDataAs", _showDataAs, ShowDataAs.Normal);
                c.SetOptionalInteger("baseField", _baseField, -1);
                c.SetOptionalInteger("baseItem", _baseItem, 1048832);
                c.SetOptionalUInteger("numFmtId", numFmtId);
                c.WriteOptionalElement(this._extLst);
            }
        }
    }
}